package com.example.sellqq.util;

import com.example.sellqq.constant.ResourceConstant;

import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.nio.charset.StandardCharsets;
import java.util.Calendar;
import java.util.Random;

/**
 * @Author: thekernel
 * @Date: 2020/4/20 10:16
 * @Version: 1.0
 * @Description: 文件操作
 */
public class FileUtils {

    public static String generateRandomFilename(){
        String RandomFilename = "";
        Random rand = new Random();  //生成随机数
        int random = rand.nextInt();

        Calendar calCurrent = Calendar.getInstance();
        int intDay = calCurrent.get(Calendar.DATE);
        int intMonth = calCurrent.get(Calendar.MONTH) + 1;
        int intYear = calCurrent.get(Calendar.YEAR);
        String now = intYear + "_" + intMonth + "_" +
                intDay + "_";

        RandomFilename = now + (random > 0 ? random : (-1) * random);

        return RandomFilename;
    }

    public static String getAnnouncement() {
        String jsonStr;
        try {
            Reader reader = new InputStreamReader(new FileInputStream(ResourceConstant.ABSOLUTE_ANNOUNCEMENT_FILE_PATH), StandardCharsets.UTF_8);
            int ch;
            StringBuffer sb = new StringBuffer();
            while ((ch = reader.read()) != -1) {
                sb.append((char) ch);
            }
            reader.close();
            jsonStr = sb.toString();
            return jsonStr;
        } catch (Exception e) {
            e.printStackTrace();
            return "";
        }
    }

    public static String getGroup() {
        String jsonStr;
        try {
            Reader reader = new InputStreamReader(new FileInputStream(ResourceConstant.ABSOLUTE_GROUP_FILE_PATH), StandardCharsets.UTF_8);
            int ch;
            StringBuffer sb = new StringBuffer();
            while ((ch = reader.read()) != -1) {
                sb.append((char) ch);
            }
            reader.close();
            jsonStr = sb.toString();
            return jsonStr;
        } catch (Exception e) {
            e.printStackTrace();
            return "";
        }
    }


}
